﻿using System.Xml.Linq;
using Monty.Xdt;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace Xdt.NantTasks
{
	[TaskName("xdt")]
	public class Transform : Task
	{
		#region Task Attributes

		[TaskAttribute("source", Required = true)]
		[StringValidator(AllowEmpty = false)]
		public string SourceFile { get; set;  }

		[TaskAttribute("transform", Required = true)]
		[StringValidator(AllowEmpty = false)]
		public string TransformFile { get; set; }

		[TaskAttribute("destination", Required = true)]
		[StringValidator(AllowEmpty = false)]
		public string DestinationFile { get; set; }

		#endregion

		#region Execute

		protected override void ExecuteTask()
		{
			var sourceDocument = XDocument.Load(SourceFile);
			var transformDocument = XDocument.Load(TransformFile);

			var xdtTransformer = new XdtTransformer();

			var destinationDocument = xdtTransformer.Transform(sourceDocument, transformDocument);

			destinationDocument.Save(DestinationFile);
		}

		#endregion
	}
}
